package com.ybu;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

public class HashSetDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        System.out.println(list);
        ArrayList<String> newlist = new ArrayList<>((new HashSet<String>(list)));
        System.out.println(newlist);
        //创建hashset对象
        HashSet<String> hashSet = new HashSet<>();
        //保存数据
        hashSet.add("zhangsan");
        hashSet.add("lisi");
        hashSet.add("wangwu");
        hashSet.add("zhaoliu");
        hashSet.add("qianqi");
        hashSet.add("lili");
        System.out.println(hashSet);
        //取数据
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);

        }
        //增强for循环取数据
        for (String name : hashSet) {
            System.out.println(name);
        }
        //lambda表达式取数据
        hashSet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        hashSet.forEach(s-> System.out.println(s));
        hashSet.forEach(System.out::println);
    }
}
